{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-10T09:30:57.332950Z",
     "start_time": "2025-10-10T09:30:57.329360Z"
    }
   },
   "source": [
    "import os\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "kimi_model = os.getenv('KIMI_MODEL')"
   ],
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T09:32:09.844208Z",
     "start_time": "2025-10-10T09:32:09.840805Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langgraph.graph import add_messages, StateGraph, START, END\n",
    "from typing_extensions import TypedDict, Annotated"
   ],
   "id": "57da3d4e760654af",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T09:32:11.346037Z",
     "start_time": "2025-10-10T09:32:11.342736Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class State(TypedDict):\n",
    "    messages: Annotated[list, add_messages] # add Reduce\n",
    "\n",
    "graph_builder = StateGraph(State)"
   ],
   "id": "fecc59cea2f89f41",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T09:32:12.203781Z",
     "start_time": "2025-10-10T09:32:12.199411Z"
    }
   },
   "cell_type": "code",
   "source": [
    "llm = ChatOpenAI(model=kimi_model)\n",
    "\n",
    "def chatbot(state: State) -> State:\n",
    "    response = llm.invoke(state['messages'])\n",
    "    return {\"messages\": response}\n",
    "\n",
    "graph_builder.add_node(\"chatbot\", chatbot)\n",
    "graph_builder.add_edge(START, \"chatbot\")\n",
    "graph_builder.add_edge(\"chatbot\", END)\n",
    "\n",
    "graph = graph_builder.compile()"
   ],
   "id": "720f963cd3677122",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-10T10:08:53.275774Z",
     "start_time": "2025-10-10T10:08:34.035620Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def stream_graph_updates(user_input: str):\n",
    "    init_state = {\n",
    "        \"messages\": [\n",
    "            {\n",
    "                \"role\": \"user\",\n",
    "                \"content\": user_input,\n",
    "            },\n",
    "        ],\n",
    "    }\n",
    "    for dic in graph.stream(init_state):\n",
    "        for msgs in dic.values():\n",
    "            # msgs是一个字典{\"messages\":AIMessage}\n",
    "            print(\"Assistant: \"+msgs[\"messages\"].content)\n",
    "while True:\n",
    "    try:\n",
    "        user_input = input(\"User: \")\n",
    "        if user_input.lower() in [\"quit\", \"exit\", \"q\"]:\n",
    "            print(\"Goodbye\")\n",
    "            break\n",
    "        print(\"User: \"+user_input)\n",
    "        stream_graph_updates(user_input)\n",
    "    except:\n",
    "        user_input = \"What do u know about LangGraph?\"\n",
    "        print(\"User: \"+user_input)\n",
    "        stream_graph_updates(user_input)\n",
    "        break"
   ],
   "id": "1bb2afdb25f02916",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: 你了解巨蟹座吗？\n",
      "Assistant: 当然了解。巨蟹座（6月21日–7月22日）是水象星座，由月亮守护，在占星学里常被形容为“壳硬心软”的典型。\n",
      "\n",
      "几个关键词可以概括巨蟹的核心特质：\n",
      "\n",
      "- **情感深沉**：巨蟹不是情绪最多，而是情绪**最深**。他们容易“共情过载”，别人一句无心话，他们能在心里回放三天。\n",
      "- **领地意识**：巨蟹的“壳”不仅是自我保护，更是他们对“归属感”的执念。家、家人、熟悉的气味、旧物，都是他们的精神锚点。\n",
      "- **记忆宫殿**：他们自带“情感硬盘”，能把十年前你随口说的一句话记得分毫不差，并在下次争吵时精准回放。\n",
      "- **照顾型人格**：巨蟹表达爱的方式是“让你吃得饱、穿得暖、情绪被接住”。但容易陷入“我为你做了这么多，你怎么不懂”的委屈循环。\n",
      "- **防御性幽默**：当巨蟹感到不安时，会先用自嘲或冷幽默试探水温，再决定要不要把心门打开一条缝。\n",
      "\n",
      "一句话总结：  \n",
      "**巨蟹的爱像潮汐，退潮时看似冷漠，其实只是在悄悄计算，下一次涨潮能不能把你安全地托住。**\n",
      "\n",
      "如果你是巨蟹，或者身边有巨蟹，可以告诉我具体场景，我可以帮你拆解他们“壳”背后的真实需求。\n",
      "Goodbye\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "e8ac65c0a42cb7da"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
